#!/bin/sh
#
# Written by James Rich james@chowhouse.com

# Assumed to be in public domain per our submission guidelines
# Modified by the SlackBuilds.org project

PRGNAM=gdm
VERSION=2.16.1
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT

cd $TMP || exit 1
rm -rf $PRGNAM-$VERSION
tar xzvf $CWD/$PRGNAM-$VERSION.tar.gz || exit 1
cd $PRGNAM-$VERSION || exit 1

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc/X11 \
  --localstatedir=/var/lib/gdm \
  --docdir=/usr/doc \
  --with-dmconfdir=/etc/X11/gdm \
  --disable-scrollkeeper \
  --enable-console-helper=no \
  --enable-static=no \
  || exit 1

make || exit 1
make install DESTDIR=$PKG

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a README COPYING AUTHORS TODO docs/C/* \
  $PKG/usr/doc/$PRGNAM-$VERSION

# Simple session files designed to work with Slackware:
mkdir -p $PKG/etc/X11/gdm/Sessions
cp $CWD/Sessions/* $PKG/etc/X11/gdm/Sessions
chmod 0755 $PKG/etc/X11/gdm/Sessions/*

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
